/**
 * Copyright (C) 2012 The SkyTvOS Project
 *
 * Version     Date           Author
 * ─────────────────────────────────────
 *           2014年12月29日         cassie
 *
 */

package com.skyworth.util;

import android.content.Context;
import android.graphics.Canvas;
import android.view.ViewGroup.LayoutParams;
import android.widget.FrameLayout;
import android.widget.ImageView;

public class FocusView extends ImageView
{
    private int posX, posY;
    private int stepX, stepY;
    private int destWidth, destHeight;
    private int stepWidth, stepHeight;
    private float stepCount = 4.0f;
    private boolean executeAnim = false;

    public FocusView(Context context)
    {
        super(context);
        // TODO Auto-generated constructor stub
    }

    // 移动到指定位置
    public void changeFocusPos(int posX, int posY, int destWidth, int destHeight)
    {
        executeAnim = true;
        this.posX = posX;
        this.posY = posY;
        this.destWidth = destWidth;
        this.destHeight = destHeight;
        initStep();
        invalidate();
    }

    // 移动到指定位置
    public void changeFocusPos(int destWidth, int destHeight)
    {
        executeAnim = true;
        this.destWidth = destWidth;
        this.destHeight = destHeight;
        initStep();
        invalidate();
    }

    // 直接设置位置到指定位
    public void setFocusPos(int posX, int posY, int destWidth, int destHeight)
    {
        executeAnim = false;
        FrameLayout.LayoutParams frameParams = (FrameLayout.LayoutParams) getLayoutParams();
        frameParams.leftMargin = posX;
        frameParams.topMargin = posY;
        frameParams.width = destWidth;
        frameParams.height = destHeight;
        this.setLayoutParams(frameParams);
    }

    @Override
    protected void onDraw(Canvas canvas)
    {
        // TODO Auto-generated method stub
        super.onDraw(canvas);
        if (!executeAnim)
            return;
        LayoutParams params = this.getLayoutParams();
        if (params instanceof FrameLayout.LayoutParams)
        {
            FrameLayout.LayoutParams frameParams = (FrameLayout.LayoutParams) params;
            if (getWidth() == destWidth && getHeight() == destHeight && getLeft() == posX
                    && getTop() == posY)
                return;
            // 处理宽度
            if (getWidth() != destWidth)
            {
                if (Math.abs(getWidth() - destWidth) < Math.abs(stepWidth))
                    frameParams.width = destWidth;
                else
                    frameParams.width = getWidth() + stepWidth;
            }
            // 处理高度
            if (getHeight() != destHeight)
            {
                if (Math.abs(frameParams.height - destHeight) < Math.abs(stepHeight))
                    frameParams.height = destHeight;
                else
                    frameParams.height = getHeight() + stepHeight;
            }
            // 处理横坐标
            if (frameParams.leftMargin != posX)
            {
                if (Math.abs(getLeft() - posX) < Math.abs(stepX))
                    frameParams.leftMargin = posX;
                else
                    frameParams.leftMargin += stepX;
            }
            // 处理纵坐标
            if (frameParams.topMargin != posY)
            {
                if (Math.abs(getTop() - posY) < Math.abs(stepY))
                    frameParams.topMargin = posY;
                else
                    frameParams.topMargin += stepY;
            }
            this.setLayoutParams(frameParams);
            invalidate();
        }
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh)
    {
        // TODO Auto-generated method stub
        initStep();
        super.onSizeChanged(w, h, oldw, oldh);
    }

    private void initStep()
    {
        if (posX == getLeft())
            stepX = 0;
        else
        {
            stepX = (int) ((posX - getLeft()) / stepCount);
            if (stepX == 0)
                stepX = (posX - getLeft()) / Math.abs(posX - getLeft());
        }

        if (posY == getTop())
            stepY = 0;
        else
        {
            stepY = (int) ((posY - getTop()) / stepCount);
            if (stepY == 0)
                stepY = (posY - getTop()) / Math.abs(posY - getTop());
        }

        if (destWidth == getWidth())
            stepWidth = 0;
        else
        {
            stepWidth = (int) ((destWidth - getWidth()) / stepCount);
            if (stepWidth == 0)
                stepWidth = (destWidth - getWidth()) / Math.abs(destWidth - getWidth());
        }

        if (destHeight == getHeight())
            stepHeight = 0;
        else
        {
            stepHeight = (int) ((destHeight - getHeight()) / stepCount);
            if (stepHeight == 0)
                stepHeight = (destHeight - getHeight()) / Math.abs(destHeight - getHeight());
        }
    }
}
